package com.servlet;

import com.dao.Impl.CateGoryDaoImp;
import com.dao.Impl.GoodsDaoImpl;
import com.dao.Impl.StockDaoImpl;
import com.entity.Category;
import com.entity.Goods;
import com.entity.PageBean;
import com.entity.Stock;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/stockController/*")
public class StockServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = MyUtil.getMethon(request);
        String target = null;
        switch (method) {
            case "addStock":
                addStock(request, response);
                break;
            case "delStock":
                delStock(request, response);
                break;
            case "modifyStock":
                modifyStock(request, response);
                break;
            case "showInfo":
                target = showInfo(request, response);
                break;
            case "showPage":
                showPage(request, response);
                break;
            default:
                break;
        }
        if (target != null) {
            request.getRequestDispatcher(target).forward(request, response);
        }
    }

    private void showPage(HttpServletRequest request, HttpServletResponse response) {
        PageBean pageBean = new PageBean();
        TransBean.populate(pageBean, request.getParameterMap());
        new StockDaoImpl().findByPage(pageBean);
        MyUtil.jsonWriter(response, pageBean);
    }

    private String showInfo(HttpServletRequest request, HttpServletResponse response) {
        List<Category> cgs = new CateGoryDaoImp().findAll();
        List<Goods> goods = new GoodsDaoImpl().findAll();
        request.setAttribute("cgs", cgs);
        request.setAttribute("goods", goods);
        return "/warehouse/outStock.jsp";
    }

    private void addStock(HttpServletRequest request, HttpServletResponse response) {
        Stock stock = new Stock();
        TransBean.populate(stock, request.getParameterMap());
        int i = new StockDaoImpl().addStock(stock);
        MyUtil.jsonWriter(response, i);
    }

    private void modifyStock(HttpServletRequest request, HttpServletResponse response) {
        Stock stock = new Stock();
        TransBean.populate(stock, request.getParameterMap());
        int i = new StockDaoImpl().modifyStock(stock);
        MyUtil.jsonWriter(response, i);
    }


    private void delStock(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        int i = new StockDaoImpl().delStock(id);
        MyUtil.jsonWriter(response, i);
    }
}
